package vikaa.ui;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.json.JSONObject;

import com.vikaa.pullview.PullToRefreshView;
import com.vikaa.pullview.PullToRefreshView.OnFooterRefreshListener;
import com.vikaa.pullview.PullToRefreshView.OnHeaderRefreshListener;
import com.weibo.sdk.android.api.ActivityInvokeAPI;

import vikaa.adapter.AdapterMemberDetail;
import vikaa.bean.Member_item;
import vikaa.bean.WowoTwitter_item;
import vikaa.timelinepackage.AsyLoadImg;
import vikaa.timelinepackage.Timeline;
import vikaa.tools.DialogService;
import vikaa.tools.HttpUtil;
import vikaa.wowo.R;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class WowoMemberDetail extends Activity implements OnItemClickListener, OnHeaderRefreshListener, OnFooterRefreshListener{
	private String uid;
	private ImageView avaterImg;
	private TextView nicknameTv;
	private TextView contributionTv;
	private TextView photoNumTv;
	private LinearLayout backBtn;
	private TextView checWeibokBtn;
	private TextView inviteBtn;
	ArrayList<WowoTwitter_item> plist = new ArrayList<WowoTwitter_item>();
	private PullToRefreshView mPullToRefreshView;
	private GridView mGridView;
	private AdapterMemberDetail adapter;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.wowo_member_detail_info);
		uid = getIntent().getExtras().getString("uid");
		initview();
		DialogService.showProgressDialog(WowoMemberDetail.this, getString(R.string.get_profile));
		new GetInfoThread(Timeline.space_id, uid).start();
	}
	private void initview(){
		inviteBtn = (TextView)findViewById(R.id.invite_Btn);
		inviteBtn.setVisibility(View.INVISIBLE);
		avaterImg = (ImageView) findViewById(R.id.avater);
		nicknameTv = (TextView) findViewById(R.id.nickname);
		contributionTv = (TextView) findViewById(R.id.contribution);
		photoNumTv = (TextView) findViewById(R.id.photo_num);
		backBtn = (LinearLayout) findViewById(R.id.backBtn);
		backBtn.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				finish();
			}
		});
		checWeibokBtn = (TextView) findViewById(R.id.check_weibo_Btn);
		checWeibokBtn.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				ActivityInvokeAPI.openUserInfoByUid(WowoMemberDetail.this, getIntent().getExtras().getString("sina_id"));
			}
		});
		mPullToRefreshView = (PullToRefreshView)findViewById(R.id.main_pull_refresh_view);
		mGridView = (GridView)findViewById(R.id.gridview);
		adapter = new AdapterMemberDetail(WowoMemberDetail.this, getLayoutInflater(), plist);
		mGridView.setAdapter(adapter);
		mGridView.setOnItemClickListener(this);
        mPullToRefreshView.setOnHeaderRefreshListener(this);
        mPullToRefreshView.setOnFooterRefreshListener(this);
	}
	Handler mhandler = new Handler(){
		@Override
		public void handleMessage(Message msg) {
			switch(msg.what){
			case 1:
				mPullToRefreshView.mFooterView.setVisibility(View.GONE);
				DialogService.closeProgressDialog();
				if(msg.obj != null){
					Member_item item = (Member_item) msg.obj;
					AsyLoadImg.loadAvater(HttpUtil.HttpUrlComplete(item.getProfile_image_url()), avaterImg);
					nicknameTv.setText(item.getScreen_name());
					contributionTv.setText(getString(R.string.contribution) + " " + item.getCredits());
					photoNumTv.setText(getString(R.string.photo_number) + " " + item.getCount());
					}
				adapter.notifyDataSetChanged();
				break;
			case 2:
				break;
			case 3:
				DialogService.closeProgressDialog();
				if(msg.obj != null){
					Toast.makeText(WowoMemberDetail.this, (String)msg.obj, Toast.LENGTH_SHORT).show();
				}
				else
					Toast.makeText(WowoMemberDetail.this, R.string.server_crash, Toast.LENGTH_SHORT).show();
				break;
			case 4:
				DialogService.closeProgressDialog();
				Toast.makeText(WowoMemberDetail.this, R.string.network_crash, Toast.LENGTH_SHORT).show();
				break;
			}
		}
	};
	class GetInfoThread extends Thread{
		private String space_id;
		private String uid;
		private boolean isrun = true;
		public GetInfoThread(String space_id, String uid) {
			super();
			this.space_id = space_id;
			this.uid = uid;
		}
		@Override
		public void run() {
			while(isrun){
				isrun = false;
				Map<String, String> map = new HashMap<String, String>();
				map.put("space_id", space_id);
				map.put("uid", uid);
				try{
					JSONObject jsonObj = new JSONObject(HttpUtil.postRequest(HttpUtil.BASE_URL+"space/member", map));
					if(jsonObj.getString("result").equals("1")){
						JSONObject info = jsonObj.getJSONObject("info");
						Member_item item = new Member_item();
						item.setUid(info.getString("uid"));
						item.setCredits(info.getString("credits"));
						item.setDomain(info.getString("domain"));
						item.setOauth_type(info.getString("oauth_type"));
						item.setProfile_image_url(info.getString("profile_image_url"));
						item.setScreen_name(info.getString("screen_name"));
						item.setSina_id(info.getString("sina_id"));
						item.setCount(info.getString("count"));
						for(int i=0;i<info.getJSONArray("twitters").length();i++){
							WowoTwitter_item twitter = new WowoTwitter_item();
							twitter.setThumbnail_pic(info.getJSONArray("twitters").getJSONObject(i).getString("thumbnail_pic"));
							twitter.setBmiddle_pic(info.getJSONArray("twitters").getJSONObject(i).getString("bmiddle_pic"));
							twitter.setOriginal_pic(info.getJSONArray("twitters").getJSONObject(i).getString("original_pic"));
							plist.add(twitter);
						}
						Message msg = mhandler.obtainMessage(1);
						msg.obj = item;
						msg.sendToTarget();
					}
					else if(jsonObj.getString("result").equals("0")){
						Message msg = mhandler.obtainMessage(3);
						msg.obj = jsonObj.getString("msg");
						msg.sendToTarget();
					}else{
						Message msg = mhandler.obtainMessage(4);
						msg.sendToTarget();
					}
				}catch(Exception e){
					e.printStackTrace();
				}
			}
		}
	}
	public void onFooterRefresh(PullToRefreshView view) {
		// TODO Auto-generated method stub
		
	}
	public void onHeaderRefresh(PullToRefreshView view) {
		// TODO Auto-generated method stub
		
	}
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		// TODO Auto-generated method stub
		
	}
}
